<?php
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
jimport('joomla.filesystem.file');

class QlchungController extends JController{
	function __construct($config = array()){
		parent::__construct($config);
		$this->registerTask( 'publish', 'published' );
		$this->registerTask( 'unpublish', 'unpublished' );
	}
	function display(){
		JRequest::setVar( 'layout', 'default');
		JRequest::setVar( 'view'  , 'detail');  
		
		parent::display();
	}
	function uploads($file){
		$temp =  substr(md5(date('l jS \of F Y h:i:s A').rand(0,100)),0,15).'_';
		$temp.= strtolower(str_replace(' ','_',$file['name']));
		$destImage=JPATH_SITE.DS.'uploads'.DS.'banner'.DS.$temp;
		JFile::upload($file['tmp_name'], $destImage);	
		return $temp;
	}
	function save(){		
		$post	= JRequest::get('post');	
		$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		if($cid[0]){
            $db = & JFactory::getDBO();
            $query = 'select banner from #__qlchung where id='.$cid[0];
            $db->setQuery($query);
            $image = $db->loadResult();
			
        }
		////////////////////////////////////////////////
		$oldimage = $post['oldimage'];
		$temp =  substr(md5(date('l jS \of F Y h:i:s A').rand(0,100)),0,15).'_';
		$file = JRequest::getVar('banner', null, 'files', 'array');
		if($file){
		$filename = JFile::makeSafe($temp.$file['name']); 
		$src = $file['tmp_name'];
		$dest = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $filename;
		switch(strtolower(JFile::getExt($filename) ))
		{
			case 'jpg':
			case 'png':
			case 'gif':
			case 'bmp':
			case 'jpeg':
			case 'swf':
			{
				if ( JFile::upload($src, $dest) ) {	
				$post['banner'] = $filename;	
				$path_oldimage = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $oldimage;			
				if($oldimage)	{					
					JFile::delete($path_oldimage);					
								}
		   		}
			}
		}		
		}
		/////////
		if($cid[0]){
            $db = & JFactory::getDBO();
            $query = 'select background from #__qlchung where id='.$cid[0];
            $db->setQuery($query);
            $image = $db->loadResult();
			
        }
		////////////////////////////////////////////////
		$oldimage2 = $post['oldimage2'];
		$temp =  substr(md5(date('l jS \of F Y h:i:s A').rand(0,100)),0,15).'_';
		$file = JRequest::getVar('background', null, 'files', 'array');
		if($file){
		$filename = JFile::makeSafe($temp.$file['name']); 
		$src = $file['tmp_name'];
		$dest = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $filename;
		switch(strtolower(JFile::getExt($filename) ))
		{
			case 'jpg':
			case 'png':
			case 'gif':
			case 'bmp':
			case 'jpeg':
			case 'swf':
			{
				if ( JFile::upload($src, $dest) ) {	
				$post['background'] = $filename;	
				$path_oldimage2 = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $oldimage2;			
				if($oldimage2)	{					
					JFile::delete($path_oldimage2);					
								}
		   											}
			}
		}		
		}
		/////////
		if($cid[0]){
            $db = & JFactory::getDBO();
            $query = 'select logoleft from #__qlchung where id='.$cid[0];
            $db->setQuery($query);
            $image = $db->loadResult();
			
        }
		////////////////////////////////////////////////
		$oldimage3 = $post['oldimage3'];
		$temp =  substr(md5(date('l jS \of F Y h:i:s A').rand(0,100)),0,15).'_';
		$file = JRequest::getVar('logoleft', null, 'files', 'array');
		if($file){
		$filename = JFile::makeSafe($temp.$file['name']); 
		$src = $file['tmp_name'];
		$dest = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $filename;
		switch(strtolower(JFile::getExt($filename) ))
		{
			case 'jpg':
			case 'png':
			case 'gif':
			case 'bmp':
			case 'jpeg':
			case 'swf':
			{
				if ( JFile::upload($src, $dest) ) {	
				$post['logoleft'] = $filename;	
				$path_oldimage3 = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $oldimage3;			
				if($oldimage3)	{					
					JFile::delete($path_oldimage3);					
								}
		   											}
			}
		}		
		}
		/////////
		if($cid[0]){
            $db = & JFactory::getDBO();
            $query = 'select logoright from #__qlchung where id='.$cid[0];
            $db->setQuery($query);
            $image = $db->loadResult();
			
        }
		////////////////////////////////////////////////
		$oldimage4 = $post['oldimage4'];
		$temp =  substr(md5(date('l jS \of F Y h:i:s A').rand(0,100)),0,15).'_';
		$file = JRequest::getVar('logoright', null, 'files', 'array');
		if($file){
		$filename = JFile::makeSafe($temp.$file['name']); 
		$src = $file['tmp_name'];
		$dest = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $filename;
		switch(strtolower(JFile::getExt($filename) ))
		{
			case 'jpg':
			case 'png':
			case 'gif':
			case 'bmp':
			case 'jpeg':
			case 'swf':
			{
				if ( JFile::upload($src, $dest) ) {	
				$post['logoright'] = $filename;	
				$path_oldimage4 = JPATH_SITE . DS . "uploads" . DS . "banner" . DS . $oldimage4;			
				if($oldimage4)	{					
					JFile::delete($path_oldimage4);					
								}
		   		}
			}
		}		
		}
		//////
		$model = $this->getModel('detail');
		$post['text'] = JRequest::getVar('text', '', 'post', 'string', JREQUEST_ALLOWRAW);
		$post['footer'] = JRequest::getVar( 'footer', '', 'post','string', JREQUEST_ALLOWRAW );
		if ($model->store($post)) {
			$msg = JText::_( 'Lưu thành công' );
		} else {
			$msg = JText::_( 'Có lỗi' );
		}
		$link = 'index.php?option=com_qlchung';
		$this->setRedirect($link, $msg);
	}
	function published()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa chọn gi hết' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_qlchung' );
	}
	function unpublished()
	{
		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}
		$model = $this->getModel('detail');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_qlchung' );
	}
}